package com.sykj.manage.web;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.omg.CORBA.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sykj.manage.dao.BisCustomDao;
import com.sykj.manage.service.BisCouponSerivce;
import com.sykj.manage.model.BisCustomUser;
import com.sykj.manage.model.BisShop;
import com.sykj.manage.model.BisShopCustom;
import com.sykj.util.IDGenerator;
import com.sykj.util.ManageBody;
import com.sykj.util.PageUtil;

@Controller
@RequestMapping("/bc")
public class BisCouponController {

	private static final String UPLOAD_PATH = "/static/images/";
	@Autowired
	BisCouponSerivce bisCouponSerivce;
	
	//列表显示
	@RequestMapping("/list")
	@ResponseBody
	public ManageBody getall(@RequestParam HashMap<String,Object> paramMaps,Integer offset,Integer pageSize){
		System.out.println("======s"+pageSize);
		PageHelper.startPage(offset==null?1:PageUtil.getPageNum(offset, pageSize), pageSize==null?10:pageSize, true);
		
		System.out.println("=====" + paramMaps);
		List<BisShopCustom> getall = bisCouponSerivce.getall(paramMaps);
		PageInfo<BisShopCustom> pageInfo = new PageInfo<BisShopCustom>(getall);
		return ManageBody.getData((int) pageInfo.getTotal(), pageInfo.getList(), "");
	}
	//下拉框显示 1 2 3
	@RequestMapping("/select1")
	@ResponseBody
	public ManageBody getshopnames(BisShop bisShop){
		List<BisShop> getshopnames = bisCouponSerivce.getshopnames(bisShop);
		System.out.println("===============");
		return ManageBody.getData(getshopnames.size(), getshopnames, "");
	}
	@RequestMapping("/select2")
	@ResponseBody
	public ManageBody getrecenttimes(BisShopCustom bisShopCustom){
		
		List<BisShopCustom> getrecenttimes = bisCouponSerivce.getrecenttimes(bisShopCustom);
		System.out.println("===============");
		return ManageBody.getData(getrecenttimes.size(), getrecenttimes, "");
	}
	@RequestMapping("/select3")
	@ResponseBody
	public ManageBody getenrollmodes(BisCustomUser bisCustomUser){
		List<BisCustomUser> getenrollmodes = bisCouponSerivce.getenrollmodes(bisCustomUser);
		System.out.println("===============");
		return ManageBody.getData(getenrollmodes.size(), getenrollmodes, "");
	}
	//添加
	@RequestMapping("/add")
	public String add(BisCustomUser bisCustomUser,@RequestParam("file")MultipartFile file,HttpServletRequest request,BisShopCustom bisShopCustom){
		String realPath = request.getSession().getServletContext().getRealPath(UPLOAD_PATH);
		//设置给前台内容的数据格式
		System.out.println("realPath"+realPath);
		String filename = file.getOriginalFilename();
		System.out.println("文件名称："+filename);
		File taFile = new File(realPath+"/"+filename);
		if(!taFile.exists()){
			taFile.mkdirs();
		}
		try {
			file.transferTo(taFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String fileurl = request.getContextPath()+UPLOAD_PATH+filename;
		System.out.println("文件URL"+fileurl);
		bisCustomUser.setPhoto(fileurl);//存图片路径
		
		
		//第一个是转时间，第二个是id自增长
		bisCustomUser.setBorn_date(new Date());
		bisShopCustom.setShop_id("10000001");
		bisShopCustom.setCreate_time(new Date());
		bisShopCustom.setRecent_time(new Date());
		bisCouponSerivce.addshopcustom(bisShopCustom);
//		bisCustomUser.setCustom_user_id(IDGenerator.generatorId());
		bisCouponSerivce.add(bisCustomUser);
		return "redirect:/manage/customer.html";
		
	}
	
	//删除
	@RequestMapping("/deleteType/{custom_user_id}")
	@ResponseBody
	public ManageBody deleteType(@PathVariable("custom_user_id")String custom_user_id){
		int deleteType = bisCouponSerivce.deleteType(custom_user_id);
		if (deleteType>0) {
			System.out.println("删除了"+deleteType+"数据，Id是："+custom_user_id);
			return ManageBody.getData(1, deleteType, "");
		} else {
			return ManageBody.getData(1, deleteType, "");
		}
	}
	
	//id查询
	@RequestMapping("/findByTypeId/{custom_user_id}")
	@ResponseBody
	public Object Info(@PathVariable("custom_user_id") String custom_user_id){
		//根据id获取所选对象数据
		BisCustomUser findByTypeId = bisCouponSerivce.findByTypeId(custom_user_id);
		System.out.println("-------------"+findByTypeId);
		return findByTypeId;
	}
	//id查看基本详情
		@RequestMapping("/findCustomerById/{custom_user_id}")
		@ResponseBody
		public Object info(@PathVariable("custom_user_id") String custom_user_id){
			//根据id获取所选对象数据
			BisCustomUser findCustomerById = bisCouponSerivce.findCustomerById(custom_user_id);
			System.out.println("------1111111111111-------"+findCustomerById);
			return findCustomerById;
		}
		

		@RequestMapping("/updateType")
	public String updateType(BisCustomUser bisCustomUser,@RequestParam("file")MultipartFile file,HttpServletRequest request) throws IOException{
		bisCustomUser.setBorn_date(new Date());
		String realPath = request.getSession().getServletContext().getRealPath(UPLOAD_PATH);
		//设置给前台内容的数据格式
		System.out.println("realPath"+realPath);
		String filename = file.getOriginalFilename();
		System.out.println("文件名称："+filename);
		File taFile = new File(realPath+"/"+filename);
		if(!taFile.exists()){
			taFile.mkdirs();
		}
		try {
			file.transferTo(taFile);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String fileurl = request.getContextPath()+UPLOAD_PATH+filename;
		System.out.println("文件URL"+fileurl);
		bisCustomUser.setPhoto(fileurl);//存图片路径
		int updateType = bisCouponSerivce.updateType(bisCustomUser);
		if (updateType>0) {
		System.out.println("====================修改了"+updateType);
		return "redirect:/manage/customer.html";
	} else {
		return "redirect:/manage/customer.html";
		}
	
	}
}
